home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / MArray.cc < prev    next >
C/C++ Source or Header  |  1996-10-12  |  3KB  |  162 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include "MArray.h"
  32. #include "lo-error.h"
  33.  
  34. #include "MArray-defs.h"
  35.  
  36. // One dimensional array with math ops.
  37.  
  38. // Element by element MArray by scalar ops.
  39.  
  40. template <class T>
  41. MArray<T>&
  42. operator += (MArray<T>& a, const T& s)
  43. {
  44.   DO_VS_OP2 (+=)
  45.   return a;
  46. }
  47.  
  48. template <class T>
  49. MArray<T>&
  50. operator -= (MArray<T>& a, const T& s)
  51. {
  52.   DO_VS_OP2 (-=)
  53.   return a;
  54. }
  55.  
  56. // Element by element MArray by MArray ops.
  57.  
  58. template <class T>
  59. MArray<T>&
  60. operator += (MArray<T>& a, const MArray<T>& b)
  61. {
  62.   int l = a.length ();
  63.   if (l > 0)
  64.     {
  65.       int bl = b.length ();
  66.       if (l != bl)
  67.     gripe_nonconformant ("operator +=", l, bl);
  68.       else
  69.     DO_VV_OP2 (+=);
  70.     }
  71.   return a;
  72. }
  73.  
  74. template <class T>
  75. MArray<T>&
  76. operator -= (MArray<T>& a, const MArray<T>& b)
  77. {
  78.   int l = a.length ();
  79.   if (l > 0)
  80.     {
  81.       int bl = b.length ();
  82.       if (l != bl)
  83.     gripe_nonconformant ("operator -=", l, bl);
  84.       else
  85.     DO_VV_OP2 (-=);
  86.     }
  87.   return a;
  88. }
  89.  
  90. // Element by element MArray by scalar ops.
  91.  
  92. #define MARRAY_AS_OP(OP) \
  93.   template <class T> \
  94.   MArray<T> \
  95.   operator OP (const MArray<T>& a, const T& s) \
  96.   { \
  97.     DO_VS_OP (OP); \
  98.     return MArray<T> (result, l); \
  99.   }
  100.  
  101. MARRAY_AS_OP (+)
  102. MARRAY_AS_OP (-)
  103. MARRAY_AS_OP (*)
  104. MARRAY_AS_OP (/)
  105.  
  106. // Element by element scalar by MArray ops.
  107.  
  108. #define MARRAY_SA_OP(OP) \
  109.   template <class T> \
  110.   MArray<T> \
  111.   operator OP (const T& s, const MArray<T>& a) \
  112.   { \
  113.     DO_SV_OP (OP); \
  114.     return MArray<T> (result, l); \
  115.  }
  116.  
  117. MARRAY_SA_OP(+)
  118. MARRAY_SA_OP(-)
  119. MARRAY_SA_OP(*)
  120. MARRAY_SA_OP(/)
  121.  
  122. // Element by element MArray by MArray ops.
  123.  
  124. #define MARRAY_AA_OP(FCN, OP) \
  125.   template <class T> \
  126.   MArray<T> \
  127.   FCN (const MArray<T>& a, const MArray<T>& b) \
  128.   { \
  129.     int l = a.length (); \
  130.     int bl = b.length (); \
  131.     if (l != bl) \
  132.       { \
  133.     gripe_nonconformant (#FCN, l, bl); \
  134.     return MArray<T> (); \
  135.       } \
  136.     if (l == 0) \
  137.       return MArray<T> (); \
  138.     DO_VV_OP (OP); \
  139.     return MArray<T> (result, l); \
  140.   }
  141.  
  142. MARRAY_AA_OP (operator +, +)
  143. MARRAY_AA_OP (operator -, -)
  144. MARRAY_AA_OP (product,    *)
  145. MARRAY_AA_OP (quotient,   /)
  146.  
  147. // Unary MArray ops.
  148.  
  149. template <class T>
  150. MArray<T>
  151. operator - (const MArray<T>& a)
  152. {
  153.   NEG_V;
  154.   return MArray<T> (result, l);
  155. }
  156.  
  157. /*
  158. ;;; Local Variables: ***
  159. ;;; mode: C++ ***
  160. ;;; End: ***
  161. */
  162.